package data;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;

import org.hsqldb.jdbc.JDBCDataSource;

public class DataSource {
	public static final String dbStr="jdbc:hsqldb:file:readings;shutdown=true";
	
	private static Connection conn;
	private static PreparedStatement stmt;
	private static ResultSet res = null;
	
	public static void shutdown()
	{
		try {
			if (null != res)
				res.close();
			if (null != stmt)
				stmt.close();
			if (null != conn)
				conn.close();
		} catch (Exception e) {
			System.err.println("Exception during shutdown: " + e.getClass());
			e.printStackTrace();
		}
	}
	public static domainspecific.types.Reading nextREST()
	{
		try {
			if(!res.next())
			{
				updateResultSet();
				return null;
			}
			return new domainspecific.types.Reading(res.getInt(2), new Date(res.getLong(1)), res.getDouble(3), res.getDouble(4));
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
	}
	
	public static soapclient.Reading nextSOAP()
	{
		try {
			if(!res.next())
				return null;
			else
			{
				soapclient.Reading r = new soapclient.Reading();
				r.setDate(res.getLong(1));
				r.setEnergy(res.getDouble(4));
				r.setPower(res.getDouble(3));
				r.setMeterID(res.getInt(2));
				return r;
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
	}
	
	public static void updateResultSet(long start, long end)
	{
		try {
			stmt = conn.prepareCall("SELECT date, id, power, energy FROM readings WHERE date=>? AND date <=?;");
		} catch (SQLException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		try {
			stmt.setLong(1, start);
			stmt.setLong(2, end);
			res = stmt.executeQuery();
		}
		catch(SQLException e)
		{
			e.printStackTrace();
		}
	}
	
	public static void updateResultSet(long start)
	{
		try {
			stmt = conn.prepareCall("SELECT date, id, power, energy FROM readings WHERE date=>?;");
		} catch (SQLException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		try {
			stmt.setLong(1, start);
			res = stmt.executeQuery();
		}
		catch(SQLException e)
		{
			e.printStackTrace();
		}
	}
	
	private static void updateResultSet()
	{
		try {
			stmt = conn.prepareCall("SELECT date, id, power, energy FROM readings;");
		} catch (SQLException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		try {
			res = stmt.executeQuery();
		}
		catch(SQLException e)
		{
			e.printStackTrace();
		}
	}
	
	static
	{
		JDBCDataSource source = new JDBCDataSource();

        source.setDatabase(DataSource.dbStr);
        conn=null;
        try {
			conn = source.getConnection();			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
//		try {
//			conn.createStatement().execute("SET PROPERTY 'readonly' true;");
//		} catch (SQLException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
		updateResultSet();
	}
	
	
}
